package registerFunction;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import emailUtil.RandomUtil;
import encryption.DigestUtil;
import theBeans.student;
import theDaoFactory.DaoFactory;
import theDaos.IStudentDao;

/**
 * Servlet implementation class loginServlet
 */
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//通过ifEmail的值确定登录方式
		int ifEmail=Integer.parseInt(request.getParameter("ifEmail"));
		if(ifEmail!=1)
		{
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			String passwordEncrypted=DigestUtil.sha1(password);
			
			IStudentDao studentDaoInstance=DaoFactory.getStudentDaoInstance();
			student studentOption=new student();
			studentOption=studentDaoInstance.login(username, passwordEncrypted);
			
			if(studentOption!=null)
			{
				request.getSession().setAttribute("ifLogin", 1);
				request.getSession().setAttribute("sid", studentOption.getSid());
				response.sendRedirect("showExamOptionsServlet");
			}
			else
			{
				request.setAttribute("errorInfo", "");
				request.setAttribute("toURL", "");
				request.getRequestDispatcher("errorPage.jsp").forward(request, response);
			}
		}
		else
		{
			String email=request.getParameter("email");
			IStudentDao studentDaoInstance=DaoFactory.getStudentDaoInstance();
			student studentOption=new student();
			studentOption=studentDaoInstance.findByEmail(email);
			if(studentOption!=null)
			{
				String sessionCode = (String) request.getSession().getAttribute("code");
				System.out.println(sessionCode);
				//  获取session中的验证码
				if(sessionCode != null) 
				{
					//  获取页面提交的验证码
					String inputCode = request.getParameter("code");
					System.out.println("页面提交的验证码:" + inputCode);
					if (sessionCode.toLowerCase().equals(inputCode.toLowerCase())) 
					{
						request.getSession().setAttribute("ifLogin", 1);
						request.getSession().setAttribute("sid", studentOption.getSid());
						request.getRequestDispatcher("showExamOptionsServlet").forward(request, response);
					}
					else
					{
						request.setAttribute("errorInfo", "");
						request.setAttribute("toURL", "");
						request.getRequestDispatcher("errorPage.jsp").forward(request, response);
					}
				}
				else
				{
					request.setAttribute("errorInfo", "");
					request.setAttribute("toURL", "");
					request.getRequestDispatcher("errorPage.jsp").forward(request, response);
				}
				//  移除session中的验证码
				request.removeAttribute("code");
			}
			else
			{
				request.setAttribute("errorInfo", "");
				request.setAttribute("toURL", "");
				request.getRequestDispatcher("errorPage.jsp").forward(request, response);
			}
		}
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		doGet(request, response);
	}

}
